/**
 * Project Name:cnsy-url-short-service
 * File Name:SysMenuServiceImpl.java
 * Package Name:cn.cnsy123.shorturls.business.service.impl 
 * Date: 2020年4月14日-下午3:58:11
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.cnsy123.shorturls.api.controller.result.SysMenuResult;
import cn.cnsy123.shorturls.api.controller.vo.TreeMenuVo;
import cn.cnsy123.shorturls.business.entity.SysMenuInfo;
import cn.cnsy123.shorturls.business.exception.BusinessException;
import cn.cnsy123.shorturls.business.mapper.SysMenuInfoMapper;
import cn.cnsy123.shorturls.business.service.SysMenuService;

/**
 * ClassName: SysMenuServiceImpl
 * 
 * @author yangpengfei
 * @date 2020年4月14日-下午3:58:11
 * @version 1.0
 * @since JDK 1.7
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    
    @Autowired
    private SysMenuInfoMapper sysMenuInfoMapper;

    @Override
    @Transactional(readOnly = true,propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class)
    public List<TreeMenuVo> selectTreeMenuByUserId(Long userId) {
        try {
            return selectListByStatus(Boolean.TRUE,null);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }
    
    public List<TreeMenuVo> selectListByStatus(final Boolean status,Long pid){
        List<TreeMenuVo> treeMenuVos = new ArrayList<TreeMenuVo>();
        List<SysMenuInfo> list = sysMenuInfoMapper.selectListByStatus(status.booleanValue() ? Byte.valueOf("1") : Byte.valueOf("0"),pid);
        if(!CollectionUtils.isEmpty(list)) {
            for(SysMenuInfo sysMenuInfo:list) {
                TreeMenuVo treeMenuVo = new TreeMenuVo();
                SysMenuResult sysMenu = new SysMenuResult(sysMenuInfo.getId(),sysMenuInfo.getMenuName(),
                        sysMenuInfo.getMenuUrl(),sysMenuInfo.getMenuIcon(),Integer.valueOf(sysMenuInfo.getMenuType().intValue()),
                        sysMenuInfo.getPid(),Integer.valueOf(sysMenuInfo.getSortSeq().intValue()));
                treeMenuVo.setSysMenu(sysMenu);
                treeMenuVo.setChildren(selectListByStatus(status,sysMenuInfo.getId()));
                treeMenuVos.add(treeMenuVo);
            }
        }
        return treeMenuVos;
    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class)
    public SysMenuInfo selectTreeMenuByMenuUrl(String requestUrl) {
        try {
            return sysMenuInfoMapper.selectByMenuUrl(requestUrl);
        } catch (Exception e) {
            throw new BusinessException(e);
        }
    }

}
